package app.domain.server.heartbeat;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import app.domain.server.TcpServer;
import app.domain.util.StockProtocol;

public class ServerHeartbeat {
	private TcpServer ts;
	private String clientId;
	private ScheduledExecutorService es = null;
	
	public ServerHeartbeat(TcpServer ts, String clientId) {
		this.ts = ts;
		this.clientId = clientId;
	}
	
	public void start() {
		es = Executors.newSingleThreadScheduledExecutor();
		es.scheduleAtFixedRate(new Heartbeater(), StockProtocol.HEARTGAP, StockProtocol.HEARTGAP, TimeUnit.MILLISECONDS);
	}
	
	class Heartbeater implements Runnable {
		@Override
		public void run() {
			ts.sendMessage(clientId, StockProtocol.HEARTBEAT);
		}
	}

}
